<?php
//CHANGE INDEX __construct
if (!defined('BASEPATH'))
   exit('No direct script access allowed');
session_start();
ini_set("session.cookie_lifetime","14400");
class Contest_upload extends MX_Controller {

   private $module = 'contest_upload';
   private $table = 'contest_upload';

   function __construct() {
      parent::__construct();
      $this->load->model($this->module . '_model', 'model');
      if ($this->uri->segment(1) == 'admincp') {
         if ($this->uri->segment(2) != 'login') {
            if (!$this->session->userdata('userInfo')) {
               header('Location: ' . PATH_URL . 'admincp/login');
               return false;
            }
         }
         $this->template->set_template('admin');
         $this->template->write('title', 'Admin Control Panel');
      } else {
         if(!isset($_SESSION['profile'])){
           require 'application/libraries/facebook.php';
           $facebook = new Facebook(array('appId'  => FB_APP_ID, 'secret' => FB_SECRET));
           $signed_request = $facebook->getSignedRequest();
           $user_profile = $facebook->api('/me');
          
           $_SESSION['profile'] =  $user_profile;
           $_SESSION['fb_id']= $signed_request['user_id'];
           } 
      }
   }

   /* ------------------------------------ Admin Control Panel ------------------------------------ */

   public function admincp_index() {
      $default_func = 'created';
      $default_sort = 'DESC';

      $data = array(
          'module' => $this->module,
          'default_func' => $default_func,
          'default_sort' => $default_sort,
          'weeks' => $this->model->get_list_weeks()
      );
       
      $this->template->write_view('content', 'BACKEND/index', $data);
      $this->template->render();
   }


   public function admincp_save() {
      //Upload Image
      $fileName = array('image' => '');
            if ($_FILES) {
                foreach ($fileName as $k => $v) {
                    if (isset($_FILES['fileAdmincp']['error'][$k])) {
                        $typeFileImage = strtolower(substr($_FILES['fileAdmincp']['type'][$k], 0, 5));
                        if ($typeFileImage == 'image' || $typeFileImage == 'video') {
                            $tmp_name[$k] = $_FILES['fileAdmincp']["tmp_name"][$k];
                            $file_name[$k] = $_FILES['fileAdmincp']['name'][$k];
                            $ext = strtolower(substr($file_name[$k], -4, 4));
                            if ($ext == 'jpeg') {
                                $fileName[$k] = time() . '_' . SEO(substr($file_name[$k], 0, -5)) . '.jpg';
                            } else {
                                $fileName[$k] = time() . '_' . SEO(substr($file_name[$k], 0, -4)) . $ext;
                            }
                        } else {
                            print 'Only upload Image.';
                            exit;
                        }
                    }
                }
            }
      //End Upload Image

      if ($this->model->saveManagement($fileName)) {
         //Upload Image
         if ($_FILES) {
            if (isset($_FILES['fileAdmincp']['error'])) {
                 foreach ($fileName as $k => $v) {
                   $upload_path = BASEFOLDER . 'static/uploads/contest/';
                     move_uploaded_file($tmp_name[$k], $upload_path . $fileName[$k]);
                 }
            }
         }
         //End Upload Image
         print 'success';
         exit;
      }
   }

   public function admincp_update($id = 0) {
      $result[0] = array();
      if ($id != 0) {
         $result = $this->model->getDetailManagement($id);
      }
      $data = array(
          'result' => $result[0],
          'module' => $this->module,
          'id' => $id,
          'users' => $this->model->list_users(),
          'province' => $this->model->get_province(),
      );
      $this->template->write_view('content', 'BACKEND/ajax_editContent', $data);
      $this->template->render();
   }

   public function admincp_delete() {
      if ($this->input->post('id')) {
         $id = $this->input->post('id');
         $result = $this->model->getDetailManagement($id);
         $this->db->where('id', $id);
         if ($this->db->delete(PREFIX . $this->table)) {
            return true;
         }
      }
   }

   public function admincp_ajaxLoadContent() {
	
      $this->load->library('AdminPagination');
      $config['total_rows'] = $this->model->getTotalsearchContent();
      $config['per_page'] = $this->input->post('per_page');
      $config['num_links'] = 3;
      $config['func_ajax'] = 'searchContent';
      $config['start'] = $this->input->post('start');
      $this->adminpagination->initialize($config);

      $result = $this->model->getsearchContent($config['per_page'], $this->input->post('start'));
    
      $data = array(
          'result' => $result,
          'per_page' => $this->input->post('per_page'),
          'start' => $this->input->post('start'),
          'module' => $this->module
      );
      $this->session->set_userdata('start', $this->input->post('start'));
      $this->load->view('BACKEND/ajax_loadContent', $data);
   }

   public function admincp_ajaxUpdateWinnerStatus() {
      if ($this->input->post('winner') == 0) {
         $winner = 1;
      } else {
         $winner = 0;
      }
      $data = array(
          'is_winner' => $winner
      );
      $this->db->where('id', $this->input->post('id'));
      $this->db->update(PREFIX . $this->table, $data);

      $update = array(
          'is_winner' => $winner,
          'id' => $this->input->post('id'),
          'module' => $this->module
      );
      $this->load->view('BACKEND/ajax_updateWinnerStatus', $update);
   }
   public function admincp_ajaxUpdatePizzaStatus() {
      if ($this->input->post('winner_choose') == 0) {
         $winner_choose = 1;
      } else {
         $winner_choose = 0;
      }
      $data = array(
          'winner_choose' => $winner_choose
      );
      $this->db->where('id', $this->input->post('id'));
      $this->db->update(PREFIX . $this->table, $data);

      $update = array(
          'winner_choose' => $winner_choose,
          'id' => $this->input->post('id'),
          'module' => $this->module
      );
      $this->load->view('BACKEND/ajax_updatePizzaStatus', $update);
   }

   public function admincp_ajaxUpdateStatus() {
      if ($this->input->post('status') == 0) {
         $status = 1;
      } else {
         $status = 0;
      }
      $data = array(
          'status' => $status
      );
      $this->db->where('id', $this->input->post('id'));
      $this->db->update(PREFIX . $this->table, $data);

      $update = array(
          'status' => $status,
          'id' => $this->input->post('id'),
          'module' => $this->module
      );
      $this->load->view('BACKEND/ajax_updateStatus', $update);
   }

   public function admincp_ajaxUpdateNoibat() {
      if ($this->input->post('status') == 0) {
         $status = 1;
      } else {
         $status = 0;
      }
      $data = array(
          'noibat' => $status
      );
      $this->db->where('id', $this->input->post('id'));
      $this->db->update(PREFIX . $this->table, $data);

      $update = array(
          'status' => $status,
          'id' => $this->input->post('id'),
          'module' => $this->module
      );
      $this->load->view('BACKEND/ajax_updateNoibat', $update);
   }
   
   /* ------------------------------------ End Admin Control Panel -------------------------------- */

   function index() {
   
         $fb_id = $_SESSION['fb_id'];

        $check = $this->model->check_user($fb_id);
        if($check < 1 )
        {
            $this->model->insert_user();
        }
        $data= array(
            'fb_id' => md5($fb_id)
        );
        $this->template->write_view('content', 'FRONTEND/landing',$data);
        $this->template->render(); 
   
  
  }
   function contest_index()
   {
        $u=$_SESSION['profile'];
        $user_data = $this->model->get_user($u['id']);
        $data= array(
            'user_data' => $user_data
        );
        $this->template->write_view('content', 'FRONTEND/contest',$data);
        $this->template->render(); 
   }
   public function get_contest()
   {
       $action = trim($this->input->get_post('action'));
     
       switch($action)
       {
            case 'home-page':
                redirect(site_url('contest_upload')); 
                break;
            case 'collection-list':
                $this->contest_list();
                break;
            case 'collection-detail':
                $this->contest_detail();
                break;
            case "post-contest":
                $this->insert_contest();
                break;
            case "post-comment":
                $this->insert_comment();
                break;
            case "insert-like":
                $this->insert_like();
                break;
            case "list-winner":
                $this->list_winner();
                break;
            case "list-week":
                $this->list_week();
                break;
            case 'upload-wall-start':
				$this->upload_wall_start();
				break;
            case 'upload-wall-end':
				$this->upload_wall_end();
				break;
			case 'list-name':
				$this->list_name();
				break;
           
            				
					
       }
   }
   
   public function list_name()
   {
		$result = $this->model->list_name();
		 header_xml();
        if ($result) {
            foreach ($result as $dt) {
                echo '<item><![CDATA[',$dt->fullname,']]></item>';
            }
        }

        footer_xml();
   }
   
  public function contest_list() {
        $weeks = $this->input->get_post('week');
        $page = $this->input->get_post('page');
        $key = $this->input->get_post('key');
        $result = $this->model->list_contest($weeks,$page,$key);
        
        header_xml();
        if (!empty($result)) {
            $total = $this->model->get_total_contest($weeks,$key);
            echo '<total>' . "<![CDATA[" . $total . "]]>" . '</total>';
            foreach ($result as $dt) {
                $thumb = PATH_URL.DIR_UPLOAD_CONTEST.$dt->images;
                echo '<item>';
                echo '<id>' . "<![CDATA[" . $dt->id . "]]>" . '</id>';
                echo '<thumb><![CDATA[' .PATH_URL.'static/images/tim/timthumb.php?src='.$thumb.'&w=140&h=107&zc=1&q=100' . ']]></thumb>';
                echo '</item>';
            }
        }

        footer_xml();
      
   }
   public function seach_contest()
   {
     $key = $this->input->get_post('key');
     $weeks = $this->input->get_post('weeks');
     $result = $this->model->list_contest_seach($key,$weeks);
     header_xml();
        if (!empty($result)) {
           
            foreach ($result as $dt) {
                $thumb = PATH_URL.DIR_UPLOAD_CONTEST.$dt->images;
                echo '<item>';
                echo '<id>' . "<![CDATA[" . $dt->id . "]]>" . '</id>';
                echo '<images><![CDATA[' .PATH_URL.'static/images/tim/timthumb.php?src='.$thumb.'&w=140&h=120&zc=1&q=100' . ']]></images>';
                echo '</item>';
            }
        }

    footer_xml();
   }
    public function contest_detail()
    {
        $id = $this->input->get_post('id');
        $data= $this->model->getContestDetail($id);
        $u=$_SESSION['profile'];
        $user_data = $this->model->get_user($u['id']);
        $link_share = "";
        $title_share = 'Pizza Hut VN - Sáng tạo tiệc cuối năm cùng Cheesy Bites';
        $description_share = 'Thỏa sức sáng tạo, rinh quà hấp dẫn với game hot nhất mùa hè này “Sáng tạo tiệc cuối năm cùng Cheesy Bites”. Chơi ngay kẻo lỡ!';
        $images = PATH_URL.'static/images/tim/timthumb.php?src='.PATH_URL.DIR_UPLOAD_CONTEST.$data->images.'&w=80&h=80&zc=1&q=100';
        $url = FB_REDIRECT_PAGE;
        $link_share = 'https://www.facebook.com/sharer/sharer.php?s=100&p%5bmedium%5d=106&p%5btitle%5d='.rawurlencode($title_share).'&p%5bsummary%5d='.rawurlencode($description_share).'&p%5burl%5d='.rawurlencode($url).'&p%5bimages%5d%5b0%5d='.$images;
        $check_like = $this->model->is_like($user_data[0]->id,$id);
        $check_like_ip = $this->model->is_like_ip(getIP(),$id);
        header_xml();
        if (!empty($data)) {
             $thumb = PATH_URL.DIR_UPLOAD_CONTEST.$data->images;
             $count_view = $data->view + 1;
             $this->model->incre_view($count_view,$id);
      
            echo '<id>' . "<![CDATA[" . $data->id . "]]>" . '</id>';
            echo '<fullname><![CDATA[',$data->fullname ,']]></fullname>';
             echo '<linkfacebook><![CDATA[http://facebook.com/',$data->fbid,']]></linkfacebook>';
            echo '<title>' . "<![CDATA[" . $data->title . "]]>" . '</title>';
            echo '<share>' . "<![CDATA[" . $link_share . "]]>" . '</share>';
            echo '<vote><![CDATA[' . $data->like . ']]></vote>';
      
            if($check_like > 0 || $check_like_ip > 5)
            {
                 echo '<isvote>' . "<![CDATA[" . "false" . "]]>" . '</isvote>';
            }
            else
            {
                 echo '<isvote>' . "<![CDATA[" . "true" . "]]>" . '</isvote>';
            }
            echo '<thumb><![CDATA[' .PATH_URL.'static/images/tim/timthumb.php?src='.$thumb.'&w=455&h=343&zc=1&q=100' . ']]></thumb>';
            $comments= $this->model->get_comments($id);

             echo '<comments>' ;
            if (!empty($comments)) {
                
                foreach($comments as $dt)
                {
                    echo '<item>';
                        echo '<user>' . "<![CDATA[" . $dt->username . "]]>" . '</user>';
                        echo '<content>' . "<![CDATA[" . $dt->content . "]]>" . '</content>';
                        echo '<date>' . "<![CDATA[" . getVNDateTime($dt->created) . "]]>" . '</date>';
                     echo '</item>';
                 }
            } 
            echo '</comments>' ;
           
        }
        footer_xml();
    }
   public function insert_contest()
   {
     $title = $this->input->get_post('name');
     $images = $this->input->get_post('image');
  	 $u=$_SESSION['profile'];
     $user_data = $this->model->get_user($u['id']);
     $week_id=$this->model->get_weeks();
     $data = array(
            'title' => $title,
            'images' => $images,
            'user_id' =>$user_data[0]->id,
            'fb_id'=>$u['id'],  
		    'week_id'=>$week_id[0]->id,
            'ip' => getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 0
        );
       	header_xml();
        if ($this->db->insert('`cli_contest_upload', $data)) {
             echo "<status>true</status>";
        }else
        {
            	echo "<status>false</status>";
        }
        footer_xml();
   } 
   public function list_comment()
   {
      $id = $this->input->get_post('id');
      $data= $this->model->get_comments($id);
  	  header_xml();
      if (!empty($data)) {
      	foreach($data as $dt)
        {
            echo '<item>';
                echo '<user>' . "<![CDATA[" . $dt->fullname . "]]>" . '</user>';
                echo '<content>' . "<![CDATA[" . $dt->content . "]]>" . '</content>';
                echo '<date>' . "<![CDATA[" . getVNDateTime($dt->created) . "]]>" . '</date>';
             echo '</item>';
         }
       }
      footer_xml();
   }
   public function insert_comment()
   {
         $content = $this->input->get_post('comment');
         $contest_id = $this->input->post("id");
      	 $u=$_SESSION['profile'];
         $user_data = $this->model->get_user($u['id']);
         $data = array(
            'content' => $content,
            'user_id' =>$user_data[0]->id,
            'contest_id'=>$contest_id,
            'ip' => getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
       	header_xml();
        if ($this->db->insert('`cli_contest_upload_comment', $data)) {
             $contest = $this->model->getContestDetail($contest_id);
             $count_comment = $contest->comment + 1;
             $this->model->incre_comment($count_comment,$contest_id);
             $comments= $this->model->get_comments($contest_id);
                if (!empty($comments)) {
                    
                    foreach($comments as $dt)
                    {
                        echo '<item>';
                            echo '<user>' . "<![CDATA[" . $dt->username . "]]>" . '</user>';
                            echo '<content>' . "<![CDATA[" . $dt->content . "]]>" . '</content>';
                            echo '<date>' . "<![CDATA[" . getVNDateTime($dt->created) . "]]>" . '</date>';
                         echo '</item>';
                     }
                } 
       
             
        }else
        {
            	
        }
        footer_xml();
        
   }
   public function insert_like()
   {
         $contest_id = $this->input->post("id");
      	 $u=$_SESSION['profile'];
         $user_data = $this->model->get_user($u['id']);
         
         $check_like = $this->model->is_like($user_data[0]->id,$contest_id);
         if($check_like > 0)
         {
            header_xml();
            echo "<message>Bạn đã bình chọn cho bài dự thi này </message>";
            echo "<status>false</status>";
           
            footer_xml();
         }
         $check_like_ip = $this->model->is_like_ip(getIP(),$contest_id);
         if($check_like_ip > 5)
         {
            header_xml();
            echo "<message>Bạn đã bình chọn cho bài dự thi này </message>";
            echo "<status>false</status>";
            footer_xml();
         }
         $data = array(
            'like' => 1,
            'user_id' =>$user_data[0]->id,
            'fb_id'=>$u['id'],  
            'contest_id'=>$contest_id,
            'ip' => getIP(),
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
       	header_xml();
        if ($this->db->insert('`cli_contest_upload_like', $data)) {
             $contest = $this->model->getContestDetail($contest_id);
             $count_like = $contest->like + 1;
             $this->model->update_like($count_like,$contest_id);
             echo "<message>Bạn đã bình chọn cho bài dự thi thành công </message>";
             echo "<status>true</status>";
              echo "<like>".$count_like."</like>";
        }else
        {
            echo "<message>Bạn đã bình chọn cho bài dự thi không thành công </message>";
           	echo "<status>false</status>";
        }
        footer_xml();
   }
   public function insert_like_test($contest_id,$user_id)
   {
         $contest_id = $contest_id;

         $check_like = $this->model->is_like($user_id,$contest_id);
         if($check_like > 0)
         {
            header_xml();
            echo "<message>Bạn đã bình chọn cho bài dự thi này </message>";
            echo "<status>false</status>";
           
            footer_xml();
         }
         $ip = rand(1,255).'.'.rand(1,255).'.'.rand(1,255).'.'.rand(1,255);
         $check_like_ip = $this->model->is_like_ip($ip,$contest_id);
         if($check_like_ip > 5)
         {
            header_xml();
            echo "<message>Bạn đã bình chọn cho bài dự thi này </message>";
            echo "<status>false</status>";
            footer_xml();
         }
         $data = array(
            'like' => 1,
            'user_id' =>$user_id,  
            'contest_id'=>$contest_id,
            'ip' => $ip,
            'created' => date('Y-m-d H:i:s', time()),
            'status' => 1
        );
       	header_xml();
        if ($this->db->insert('`cli_contest_upload_like', $data)) {
             $contest = $this->model->getContestDetail($contest_id);
             $count_like = $contest->like + 1;
             $this->model->update_like($count_like,$contest_id);
             echo "<message>Bạn đã bình chọn cho bài dự thi thành công </message>";
             echo "<status>true</status>";
             echo "<like>".$count_like."</like>";
        }else
        {
            echo "<message>Bạn đã bình chọn cho bài dự thi không thành công </message>";
           	echo "<status>false</status>";
        }
        footer_xml();
   }
   public function list_week()
   {
     $weeks = $this->model->get_list_weeks();
     $active = 0;
     header_xml();
		if(!empty($weeks))
		{
			
			foreach($weeks as $dt)
			{
			      $active = $dt->active;
                  if($active == 1)
                  {
                     echo '<item active="1">'. "<![CDATA[" .$dt->name. "<br/>". date('d/m', strtotime($dt->datestart)) .' - '. date('d/m', strtotime($dt->dateend)) . "]]>"  . '</item>';
                  }
                  else
                  {
                      echo '<item>'. "<![CDATA[" .$dt->name. "<br/>". date('d/m', strtotime($dt->datestart)) .' - '. date('d/m', strtotime($dt->dateend)) . "]]>"  . '</item>';
                  }
			     
		    }
         }
      footer_xml();
   }
   public function list_winner()
   {
        $data_weeks = $this->model->get_list_weeks();
        header_xml();
                if(!empty($data_weeks))
        		{
        			
        			foreach($data_weeks as $dt)
        			{
        			      $active = $dt->finish;
						 
                          if($active == 1)
                          {
                             echo '<week active="1">';
                             $data = $this->model->list_winner_weeks($dt->id) ; 
							 $data_pizza = $this->model->list_winner_weeks_pizza($dt->id) ; 
                             echo '<title>'. "<![CDATA[" .$dt->name. "<br/>". date('d/m', strtotime($dt->datestart)) .' - '. date('d/m', strtotime($dt->dateend)) . "]]>"  . '</title>';
                             echo '<items>';
								   if(!empty($data_pizza))
                    		       {
                    			
                    			         foreach($data_pizza as $dt)
                    			         {
                    			                   $thumb = PATH_URL.DIR_UPLOAD_CONTEST.$dt->images;
                                                   echo '<item>';
                                                   echo '<id>' . "<![CDATA[" . $dt->id . "]]>" . '</id>';
                                    			   echo '<user>' . "<![CDATA[" . $dt->username . "]]>" . '</user>';
                                    			   echo '<thumb>' . "<![CDATA[" .PATH_URL.'static/images/tim/timthumb.php?src='.$thumb.'&w=86&h=86&zc=1&q=100' . "]]>" . '</thumb>';
                                                   echo '</item>';
                                         }
                                         
                                    }
                                   if(!empty($data))
                    		       {
                    			
                    			         foreach($data as $dt)
                    			         {
                    			                   $thumb = PATH_URL.DIR_UPLOAD_CONTEST.$dt->images;
                                                   echo '<item>';
                                                   echo '<id>' . "<![CDATA[" . $dt->id . "]]>" . '</id>';
                                    			   echo '<user>' . "<![CDATA[" . $dt->username . "]]>" . '</user>';
                                    			   echo '<thumb>' . "<![CDATA[" .PATH_URL.'static/images/tim/timthumb.php?src='.$thumb.'&w=86&h=86&zc=1&q=100' . "]]>" . '</thumb>';
                                                   echo '</item>';
                                         }
                                         
                                    }
                             echo '</items>';
                             echo '</week>';
                          }
                          else
                          {
                               echo '<week active="0">';
                               echo '<title>'. "<![CDATA[" .$dt->name. "<br/>". date('d/m', strtotime($dt->datestart)) .' - '. date('d/m', strtotime($dt->dateend)). "]]>".'</title>' ;
                               echo '<items>';
                               echo '</items>';
                               echo '</week>';
                          }
        			     
        		    }
                 }
        footer_xml();
   }
   public function upload_wall_start()
   {
     require 'application/libraries/facebook.php';
     $facebook = new Facebook(array('appId'  => FB_APP_ID, 'secret' => FB_SECRET));
     $user_id = $facebook->getUser();
     $signed_request = $facebook->getSignedRequest();
     $facebook->api('me/feed/', 'POST', array(
        'message' => 'Bạn biết xếp tranh bằng Pizza?',
        'name' => 'Bạn biết xếp tranh bằng Pizza?',
        'description' => 'Thỏa sức sáng tạo, rinh quà hấp dẫn với game hot nhất mùa hè này “Sáng tạo tiệc cuối năm cùng Cheesy Bites”. Chơi ngay kẻo lỡ!',
        'caption' => 'Bạn biết xếp tranh bằng Pizza?¨',
        'picture' => PATH_URL.'static/images/share_facebook_end.png',
        'link' => 'https://apps.facebook.com/225013754346362'
    ));
   }
   public function upload_wall_end()
   {
     require 'application/libraries/facebook.php';
     $facebook = new Facebook(array('appId'  => FB_APP_ID, 'secret' => FB_SECRET));
     $user_id = $facebook->getUser();
     $signed_request = $facebook->getSignedRequest();
     $facebook->api('me/feed/', 'POST', array(
        'message' => 'Sáng tạo tiệc cuối năm cùng Cheesy Bites',
        'name' => 'Sáng tạo tiệc cuối năm cùng Cheesy Bites',
        'description' => 'Hãy trổ tài sáng tạo để xếp tranh bằng bánh Pizza và cùng vui “Sáng tạo tiệc cuối năm cùng Cheesy Bites” nhé.Bạn thử chưa?',
        'caption' => 'Sáng tạo tiệc cuối năm cùng Cheesy Bites',
        'picture' => PATH_URL.'static/images/share_facebook_end.png',
        'link' => 'https://apps.facebook.com/225013754346362'
    ));
   }
   public function upload()
   {
        $fb_id = $_SESSION['fb_id'];
        $file = uniqid(rand(1000000, 10000000));
        $img = file_get_contents('php://input');
        $filename = APPPATH.'../static/uploads/contest/' .$fb_id.$file.'.jpg';
        file_put_contents($filename, $img);
        $file_name = $fb_id.$file.'.jpg';
        echo  $file_name;
   }


   public function save_image_wall()
   {
        $fb_id = $_SESSION['fb_id'];
        $file = uniqid(rand(1000000, 10000000));
        $img = file_get_contents('php://input');
        $filename = APPPATH.'../static/uploads/upload_wall/' .$fb_id.$file.'abc.jpg';
        file_put_contents($filename, $img);
        $file_name = $fb_id.$file.'abc'.'.jpg';
        echo  $file_name;
   }
   
    public function update_thumb_default()
    {
        $result = $this->model->list_contest_new();
        foreach($result as $r){
            $data = array(
                'thumb' =>$r->thumb_default
            );
            $this->db->where('id', $r->id);
            $this->db->update('cli_contest', $data); 
        }
    }
    public function save_image_avt($fb_id)
    {
        
        
        $file_name =$fb_id.'.jpg';
	    $file_new =BASE_UPLOAD_AVATAR.$file_name;
        $in=    fopen('https://graph.facebook.com/'.$fb_id.'/picture?type=large', "rb");
        $out=   fopen($file_new, "wb");
        while ($chunk = fread($in,8192))
        {
	        fwrite($out, $chunk, 8192);
	    }
	    fclose($in);
	    fclose($out);

        return $file_name;
        
     
    }public function save_image_avatar($fb_id)
    {
        
        
        $profile= $this->model->get_profile($fb_id);
        header_xml();
            echo  "<image>". PATH_URL.'static/uploads/avatar/'.$profile[0]->avatar. "</image>";
        footer_xml();
     
    }
    public function upload_images_from_link() {
            $url= $this->input->get_post("linkimg");
    
            $tmp = explode("/",$url);
        	$file = $tmp[count($tmp) - 1];
            $file = str_replace("%","-",$file);
            
        	//echo $url."<br/>";
        	if($file)
        	{
        		
        			$h = @fopen("$url","rb");
        			$content = '';
        			if($h)
        			{
        				while(!feof($h))
        				{
        					$buffer = fgets($h, 1024);
        					$content .= $buffer;
        				}
        				fclose($h);
        			}
        			$f = fopen(BASE_UPLOAD.$file,'w');
        			fwrite($f,$content);
        			fclose($f);
        			$ff = fopen(BASE_UPLOAD.$file,'w');
        			fwrite($ff,$content);
        			fclose($ff);
                    header_xml();
                        echo '<link>'.PATH_IMAGE.'contest_tmp/' .$file.'</link>';
                    footer_xml();
        	
        	}
        	else
        	{
        		 header_xml();
                        echo '<link>edf</link>';
                 footer_xml();
        	}
                    
        }
		

}